﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BooksOnline.Models.Entities;
using BooksOnline.Services;
using NUnit.Framework;

namespace BooksOnline.Tests.ServiceFixtures
{
    [TestFixture]
    public class BookManagementServiceFixture : FixtureBase<Book>
    {
        private BookManagementService bookService = new BookManagementService();
        [TestFixtureSetUp]
        protected void SetUp()
        {
            base.SetUp();
        }

        [Test]
        public void Can_Create_Book()
        {
            var book = new Book();
            book.ISBN = "ISBN";
            book.Price = 12;
            book.Title = "Title1";

            bookService.CreateOrUpdate(book);

            Session.Evict(book);
            var result = Session.Get<Book>(book.Id);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Id, book.Id);
            Assert.AreEqual(result.ISBN, book.ISBN);
        }

        [Test]
        public void Can_Update_Book()
        {
            var book = new Book();
            book.ISBN = "ISBN";
            book.Price = 12;
            book.Title = "Title1";

            bookService.CreateOrUpdate(book);

            Session.Evict(book);
            var result = Session.Get<Book>(book.Id);

            result.ISBN = "Updated ISBN";
            result.Title = "Updated Title";

            bookService.CreateOrUpdate(result);

            Session.Evict(book);
            result = Session.Get<Book>(book.Id);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Id, book.Id);
            Assert.AreEqual(result.ISBN, "Updated ISBN");
        }
    }
}
